#

| Chapter 39 Time and Performance |

## 39.8 A Big Environment Can Slow You Down

Part of starting a new child process (starting a program, for instance) is making a copy of the environment from the parent process. Some computers, especially busy ones, aren't very fast at making new processes. (In the early 1980s, I worked on a VAX 11/750 running 4.1 BSD; the load average got above 40. Sometimes, after a command finished, it could take 10 or 20 seconds just to get the next shell prompt. Sheesh!)

Filling up your environment with lots of variables (the csh command setenv or the sh command export) can be handy. But it can slow you down - especially in shell scripts that run loops, starting lots of subprocesses.

I did a test on our 386-based computer running Interactive UNIX System V/386 Release 3.2 late one night when I was the only user logged on. First, I cleaned out my environment to around 300 characters. Then I did:

    env wc

    time
    repeat
    % env | wc -c
        335
    % set time
    % repeat 50 /bin/true
    0.0u 0.1s 0:00 15%
    0.1u 0.1s 0:00 18%
    0.0u 0.2s 0:00 20%
       ...

That started the short /bin/true shell script 50 times. I added up the system times (from the second column) and got 6.9 CPU seconds. Then I used a C shell while loop to quickly add a bunch of huge environment variables named FOO1, FOO2, and so on, like this:

    set

    @
    % set n = 0
    % while ($n < 30)
    1
    ? setenv FOO$n xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
    ? end
       ...
    % env | wc -c
        4934

and ran `repeat 50 /bin/true` again. With a 5000-character environment, it took 8.9 system CPU seconds - that's about 30% longer.

A thorough test? Nope. But if you have a big environment on a slow computer, you might run a test like this to see whether cleaning it out - replacing environment variables with shell variables, for instance - can make your subprocesses start faster.

Article 38.7 explains the problem a big environment can cause for ps. Article 2.9 shows how to start C shells more quickly.

- JP

39.7 Checking System Load: uptime |  | 39.9 Know When to Be "nice" to OTher Users...and When Not to